package com.homework;

import com.homework.config.MyApplicationContext;
import com.homework.pojo.RpcRequest;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.springframework.cglib.reflect.FastClass;
import org.springframework.cglib.reflect.FastMethod;

public class UserServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("======================>>>>>:"+msg.getClass());
        RpcRequest req = (RpcRequest) msg;
        System.out.println("req="+req);
        Class<?> clazz = Class.forName(req.getClassName());
        Object bean = MyApplicationContext.getBean(clazz);

        Class<?> aClass = bean.getClass();

        FastClass fastClass = FastClass.create(aClass);
        FastMethod method = fastClass.getMethod(req.getMethodName(), req.getParameterTypes());
        Object invoke = method.invoke(bean, req.getParameters());
        ctx.writeAndFlush(invoke);
        // 通过className找到传进来的类
        //ApplicationContext context = MyApplicationContext.getContext();
    }
}
